//
//  SettingViewController.m
//  day1
//
//  Created by 于跃隆 on 2020/4/7.
//  Copyright © 2020 于跃隆. All rights reserved.
//

#import "SettingViewController.h"
#import "SDImageCache.h"
#import "LoginNumberViewController.h"
#import "peopleMesViewController.h"
#import "myCardViewController.h"
#import "aboutMeViewController.h"
#import "BangDViewController.h"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>{
    //导航条title
    UILabel *navtitle;
}

@property (nonatomic,strong)UITableView *tableview;

@property (nonatomic,strong)NSArray *dataSourse;

@property (nonatomic,strong)UILabel *versionLab;

@property(nonatomic,copy) NSString * str;

@property(nonatomic , strong) UIView *Jbview;


@end

@implementation SettingViewController

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar removeAllSubviews];
    self.navigationController.navigationBar.hidden = NO;
    [self setNavFrame];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
-(void)setNavFrame{
    //设置导航条颜色
            _Jbview = [[UIView alloc] init];
            _Jbview.userInteractionEnabled = YES;
        //    Jbview.frame = CGRectMake(0,KSCHEIGHT(-37),kSCRATIO(self.view.frame.size.width),KSCHEIGHT(75));

            CAGradientLayer *gl = [CAGradientLayer layer];
            gl.frame = CGRectMake(0,KSCHEIGHT(0),kSCRATIO(self.view.frame.size.width),KSCHEIGHT(82));
            gl.startPoint = CGPointMake(0, 0);
            gl.endPoint = CGPointMake(1, 1);
            gl.colors = @[(__bridge id)[UIColor colorWithRed:247/255.0 green:65/255.0 blue:91/255.0 alpha:1.0].CGColor,(__bridge id)[UIColor colorWithRed:251/255.0 green:171/255.0 blue:102/255.0 alpha:1.0].CGColor];
            gl.locations = @[@(0.0),@(1.0)];
            [_Jbview.layer addSublayer:gl];
            
            [self.navigationController.navigationBar addSubview:_Jbview];
            
            //返回button
            UIButton *backBtn = [[UIButton alloc] init];
            [backBtn setBackgroundImage:[UIImage imageNamed:@"backButton"] forState:UIControlStateNormal];
            [backBtn addTarget:self action:@selector(BackClick) forControlEvents:UIControlEventTouchUpInside];
    //    [backBtn addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(BackClick)]];
            [_Jbview addSubview:backBtn];
            
            //标题文字
            navtitle = [[UILabel alloc] init];
            navtitle.text = @"设置";
            navtitle.textColor = [UIColor whiteColor];
            navtitle.font = kFONT(18);
            [_Jbview addSubview:navtitle];
            
            [_Jbview mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(KSCHEIGHT(-37));
                make.width.mas_equalTo(kSCRATIO(self.view.frame.size.width));
                make.height.mas_equalTo(KSCHEIGHT(82));
            }];
            [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(_Jbview.mas_left).mas_equalTo(kSCRATIO(20));
                make.bottom.mas_equalTo(_Jbview.bottom).mas_equalTo(KSCHEIGHT(-15));
                make.width.mas_equalTo(kSCRATIO(10));
                make.height.mas_equalTo(KSCHEIGHT(18));
            }];
            [navtitle mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(_Jbview);
                make.bottom.mas_equalTo(_Jbview.bottom).mas_equalTo(KSCHEIGHT(-12));
                
            }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UserModel *user = [UserModel sharedInstance:UserKey0];
    if ([UtilTools isBlankString:user.token]) {
        [[AppDelegate shareAppDelegate] setLoginViewController];
    }
    
    
    NSString*cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/com.hackemist.SDImageCache"];
    self.str = [NSString stringWithFormat:@"%0.2f kb",[self folderSizeAtPath:cachePath]*10*102];
    [self.view addSubview:self.tableview];
    
    //左侧导航按钮重写
//    self.navigationController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backButton"] style:UIBarButtonItemStylePlain target:self action:@selector(BackClick)];
//    [self.navigationItem setHidesBackButton:YES];
//    [self.navigationController.navigationItem setHidesBackButton:YES];
//    [self.navigationController.navigationBar.backItem setHidesBackButton:YES];
    
//    [self setNavFrame];
    
//    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_Jbview.mas_bottom).mas_equalTo(KSCHEIGHT(5));
//        make.bottom.mas_equalTo(self.view.mas_bottom).mas_equalTo(KSCHEIGHT(0));
//        make.width.mas_equalTo(kSCRATIO(self.view.frame.size.width));
//        make.height.mas_equalTo(KSCHEIGHT(self.view.frame.size.height-5));
//    }];
    
    
    //245 246 251
        self.view.backgroundColor = [UIColor colorWithRed:245/255.0 green:246/255.0 blue:251/255.0 alpha:1.0];
        
    
    
    
    UIButton *queRbtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    ViewRadius(queRbtn1, KSCHEIGHT(20));
    queRbtn1.backgroundColor = [UIColor colorWithRed:172/255.0 green:95/255.0 blue:251/255.0 alpha:1.0];
    queRbtn1.layer.shadowColor = [UIColor colorWithRed:173/255.0 green:172/255.0 blue:172/255.0 alpha:0.14].CGColor;
    queRbtn1.layer.shadowOffset = CGSizeMake(0,2);
    queRbtn1.layer.shadowOpacity = 1;
    queRbtn1.layer.shadowRadius = 4;
    
    CAGradientLayer *glT = [CAGradientLayer layer];
    glT.frame = CGRectMake(0,0,kSCRATIO(250),KSCHEIGHT(40));
    glT.startPoint = CGPointMake(0, 0);
    glT.endPoint = CGPointMake(1, 1);
    glT.colors = @[(__bridge id)[UIColor colorWithRed:247/255.0 green:65/255.0 blue:91/255.0 alpha:1.0].CGColor,(__bridge id)[UIColor colorWithRed:251/255.0 green:171/255.0 blue:102/255.0 alpha:1.0].CGColor];
    glT.locations = @[@(0.0),@(1.0)];
    

    [queRbtn1.layer addSublayer:glT];
    [self.view addSubview:queRbtn1];
    
    
    [queRbtn1 setTitle:@"确认绑定" forState:UIControlStateNormal];
    queRbtn1.titleLabel.font = MarkFont(15);
    [queRbtn1 addTarget:self action:@selector(qurenBdclick:) forControlEvents:UIControlEventTouchUpInside];
    [queRbtn1 setTitleColor:kColorFromRGBHex(0xFFFFFF) forState:UIControlStateNormal];
    [queRbtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.view.mas_top).mas_equalTo(KSCHEIGHT(270));
        make.width.mas_equalTo(kSCRATIO(250));
        make.height.mas_equalTo(KSCHEIGHT(40));
        
    }];
    
    
    
    
    
    
    
}
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, KSCHEIGHT(0), self.view.frame.size.width, KSCHEIGHT(self.view.frame.size.height) - KSCHEIGHT(88)) style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.separatorColor = [UIColor clearColor];
        
        _tableview.backgroundColor = [UIColor colorWithRed:245/255.0 green:246/255.0 blue:251/255.0 alpha:1.0];
        
        _tableview.scrollEnabled = NO;
        _tableview.sectionFooterHeight = KSCHEIGHT(5);
        _tableview.sectionHeaderHeight = KSCHEIGHT(0);
        
    }
    return _tableview;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return KSCHEIGHT(15.0);
    }
    return  0.0001;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    tableView.rowHeight = KSCHEIGHT(50);
    cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
    cell.textLabel.font = kFONT(16);
    [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(cell);
        make.left.mas_equalTo(kSCRATIO(20));
        make.top.mas_equalTo(KSCHEIGHT(17));
        
    }];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.section == 0) {
        cell.textLabel.text = @"个人信息";
        
    }else if (indexPath.section == 1){
        cell.textLabel.text = @"我的银行卡";
        
    }else if (indexPath.section == 2){
        cell.textLabel.text = @"关于我们";
        
    }else if (indexPath.section == 3){
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        cell.textLabel.text = @"清除缓存";
        cell.detailTextLabel.text = self.str;
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        peopleMesViewController *peopleM = [[peopleMesViewController alloc] init];
        navtitle.text = @"个人中心";
        [self.navigationController pushViewController:peopleM animated:NO];
        
        
    }else if (indexPath.section == 1) {
        
        myCardViewController *myCardV = [[myCardViewController alloc] init];
        navtitle.text = @"我的银行卡";
        [self.navigationController pushViewController:myCardV animated:NO];
        
       
    }else if (indexPath.section == 2) {
        
        aboutMeViewController *aboutV = [[aboutMeViewController alloc] init];
        navtitle.text = @"关于我们";
        [self.navigationController pushViewController:aboutV animated:NO];
        
       
    }else if (indexPath.section == 3) {
    
        [HWProgressHUD showMessage:[NSString stringWithFormat:@"清理了%@",self.str]];
        [[SDImageCache sharedImageCache] clearMemory];
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            
        }];
    self.str = @"0.0";
    [self.tableview reloadData];
    }
    
}


-(void)qurenBdclick:(UIButton *)btn{
//    UserModel *user = [UserModel sharedInstance:UserKey0];
//    if ([UtilTools isBlankString:user.token]) {
//
//    }else{
//
//    }
    BangDViewController *bangDVc = [[BangDViewController alloc] init];
    navtitle.text = @"绑定信息";
    [self.navigationController pushViewController:bangDVc animated:NO];
//    LoginNumberViewController *loginNV = [[LoginNumberViewController alloc] init];
//    //    [self.navigationController pushViewController:loginNV animated:YES];
//    loginNV.modalPresentationStyle = 0;
//    [self.navigationController presentViewController:loginNV animated:NO completion:^{
//
//    }];
    
}
//遍历文件夹获得文件夹大小，返回多少M
- (float )folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1000.0*1000.0);
}

//单个文件的大小
- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
//        [cell setSeparatorInset:UIEdgeInsetsZero];
//    }
//}

-(void)BackClick{
    UIViewController *viewController = self;
    navtitle.text = @"设置";
    if (viewController) {
        
        [viewController dismissViewControllerAnimated:NO completion:nil];
        
    }
    [self.navigationController popViewControllerAnimated:NO];
}



@end
